package com.lxyk.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxyk.yygh.common.result.Result;
import com.lxyk.yygh.common.utils.MD5;
import com.lxyk.yygh.hosp.service.HospitalSetService;
import com.lxyk.yygh.model.hosp.HospitalSet;
import com.lxyk.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation("获取所有医院设置")
    @GetMapping("findAll")
    public Result findAll(){
        return Result.ok(hospitalSetService.list());
    }

    @ApiOperation("逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        if (hospitalSetService.removeById(id)){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * <h2>分页条件查询</h2>
     * @param current 当前页
     * @param limit 每页显示条数
     * @param hospitalSetQueryVo 查询条件（可为空）
     * @return
     */
    @ApiOperation("分页条件查询")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable Long current,@PathVariable Long limit,@RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建page对象
        Page<HospitalSet> hospitalSetPage = new Page<>(current,limit);

        //创建条件对象
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        //判断不为空执行条件查询 医院名称
        if ( !StringUtils.isEmpty(hospitalSetQueryVo.getHosname()) ){
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }

        //判断不为空执行条件查询 医院名称
        if ( !StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()) ){
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }

        //调用方法实现分页查询
        Page<HospitalSet> page = hospitalSetService.page(hospitalSetPage, wrapper);

        //返回结果集
        return Result.ok(page);

    }


    @ApiOperation("添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){

        //初始化操作
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名
        Random random = new Random();
        //当前时间加随机数
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id查询")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        return Result.ok(hospitalSetService.getById(id));
    }

    @ApiOperation("修改医院设置")
    @PostMapping("updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation("医院设置的锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        //根据id查询
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //更新
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @ApiOperation("发送医院签名")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();

        //TODO 发送短信

        return Result.ok();

    }
}
